package cn.passlink.bletoothchat.SqlTables;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import cn.passlink.bletoothchat.Entity.mUser;

public class UserTableManager {

    private SQLiteDatabase database;

    public UserTableManager(SQLiteDatabase database) {
        this.database = database;
    }

    public void addUser(mUser user) {
        ContentValues values = new ContentValues();
        values.put("id", 1);
        values.put("phone", user.getPhone());
        values.put("username", user.getUsername());
        values.put("academy", user.getAcademy());
        values.put("grade", user.getGrade());
        values.put("classes",user.getClasses());
        values.put("area", user.getArea());
        values.put("position", user.getPosition());
        values.put("change_numbers", user.getChange_numbers());
        database.insert("user", null, values);
    }

    public void deleteUser(String userPhone) {
        String whereClause = "phone" + " = ?";
        String[] whereArgs = {String.valueOf(userPhone)};
        database.delete("user", whereClause, whereArgs);
    }

    public void updateUser(mUser user) {
        ContentValues values = new ContentValues();
        values.put("id", 1);
        values.put("phone", user.getPhone());
        values.put("username", user.getUsername());
        values.put("academy", user.getAcademy());
        values.put("grade", user.getGrade());
        values.put("classes",user.getClasses());
        values.put("area", user.getArea());
        values.put("position", user.getPosition());
        values.put("change_numbers", user.getChange_numbers());
        String whereClause = "phone" + " = ?";
        String[] whereArgs = {String.valueOf(user.getPhone())};
        database.update("user", values, whereClause, whereArgs);
    }

    public mUser getUserByPhone(String id) {
        String[] projection = {"id", "phone", "username", "academy", "grade","classes", "area", "position", "change_numbers"};
        String selection = "id" + " = ?";
        String[] selectionArgs = {id};

        Cursor cursor = database.query("user", projection, selection, selectionArgs, null, null, null);
        if (cursor.moveToFirst()) {
            mUser user = new mUser();

            user.setPhone(cursor.getString(cursor.getColumnIndexOrThrow("phone")));
            user.setUsername(cursor.getString(cursor.getColumnIndexOrThrow("username")));
            user.setAcademy(cursor.getString(cursor.getColumnIndexOrThrow("academy")));
            user.setGrade(cursor.getString(cursor.getColumnIndexOrThrow("grade")));
            user.setClasses(cursor.getString(cursor.getColumnIndexOrThrow("classes")));
            user.setArea(cursor.getString(cursor.getColumnIndexOrThrow("area")));
            user.setPosition(cursor.getString(cursor.getColumnIndexOrThrow("position")));
            user.setChange_numbers(cursor.getString(cursor.getColumnIndexOrThrow("change_numbers")));

            cursor.close();
            return user;
        }

        cursor.close();
        return null;
    }
}